﻿using System;
using System.Linq.Expressions;

using Report.Entity;
using Report.Model.ReturnModel;
using Report.IRepository;
using Report.Repository;
using Report.Helper;

namespace Report.Bussiness
{
    public class StorageBLL
    {
        private StorageHeadDAL HeadDAL = new StorageHeadDAL();
        private BarCodeDAL BarDAL = new BarCodeDAL();
        private PositionDAL PosDAL = new PositionDAL();
        private StorageParaDAL StoDAL = new StorageParaDAL();

        public string In(string barcode, string position, string uid)
        {
            //检查barcode
            在制流转条码 barcodeEntity = BarDAL.SearchByBarCode(barcode);
            if (barcodeEntity == null)
            {
                return "产品条码不是待入库状态";
            }
            //检查仓位
            仓库仓位 posEntity = PosDAL.SearchByPosition(position);
            if (posEntity == null)
            {
                return "仓位条码不存在,或者仓位在盘点";
            }
            //查询仓库类别
            标准仓库参数子表 stoEntity = StoDAL.SearchByStorage(posEntity.仓库编号);
            if (stoEntity == null)
            {
                return "仓位类别不存在";
            }
            //产生
            成品出入库主表 headEntity = new 成品出入库主表();
            成品出入库子表 detailEntity = new 成品出入库子表();
            //主表
            string startWith = stoEntity.类别编号 + "I";
            string no = getNextSeq(startWith);
            headEntity.仓库类别编号 = stoEntity.类别编号;
            headEntity.出入类别 = "入库";
            headEntity.单据编号 = no;
            headEntity.单据日期 = DateTime.Now;
            headEntity.单据类别 = barcodeEntity.单据类别;
            headEntity.仓库编号 = posEntity.仓库编号;
            headEntity.仓库核算类别 = stoEntity.核算类别;
            headEntity.经手人 = uid;
            headEntity.创建人 = uid;
            headEntity.创建时间 = DateTime.Now;
            headEntity.审核 = true;
            headEntity.审核人 = uid;
            headEntity.审核时间 = DateTime.Now;
            //子表
            detailEntity.单据编号 = no;
            detailEntity.单据序号 = no + "01";
            detailEntity.产品系统编号 = barcodeEntity.产品系统编号;
            detailEntity.颜色 = barcodeEntity.颜色;
            detailEntity.自定义1 = barcodeEntity.自定义1;
            detailEntity.自定义2 = barcodeEntity.自定义2;
            detailEntity.自定义3 = barcodeEntity.自定义3;
            detailEntity.自定义4 = barcodeEntity.自定义4;
            detailEntity.自定义5 = barcodeEntity.自定义5;
            detailEntity.自定义6 = barcodeEntity.自定义6;
            detailEntity.自定义7 = barcodeEntity.自定义7;
            detailEntity.自定义8 = barcodeEntity.自定义8;
            detailEntity.库存自定义1 = barcodeEntity.工序系统编号;
            detailEntity.库存自定义2 = "";
            detailEntity.库存自定义3 = "";
            detailEntity.库存自定义4 = "";
            detailEntity.库存自定义5 = "";
            detailEntity.条码 = barcodeEntity.条码;
            detailEntity.仓位 = posEntity.仓位编号;
            detailEntity.数量 = barcodeEntity.数量;
            detailEntity.导入序号 = barcodeEntity.流程单号;
            detailEntity.排序 = 1;
            detailEntity.审核 = true;
            detailEntity.创建人 = uid;
            detailEntity.创建时间 = DateTime.Now;

            //总规格
            string tSpk = headEntity.仓库类别编号
                    + "/" + (headEntity.仓库编号 == null ? "" : headEntity.仓库编号)
                    + "/" + (headEntity.仓库核算类别 == null ? "" : headEntity.仓库核算类别)
                    + "/" + (detailEntity.产品系统编号 == null ? "" : detailEntity.产品系统编号)
                    + "/" + (detailEntity.自定义1 == null ? "" : detailEntity.自定义1)
                    + "/" + (detailEntity.自定义2 == null ? "" : detailEntity.自定义2)
                    + "/" + (detailEntity.自定义3 == null ? "" : detailEntity.自定义3)
                    + "/" + (detailEntity.自定义4 == null ? "" : detailEntity.自定义4)
                    + "/" + (detailEntity.自定义5 == null ? "" : detailEntity.自定义5)
                    + "/" + (detailEntity.自定义6 == null ? "" : detailEntity.自定义6)
                    + "/" + (detailEntity.自定义7 == null ? "" : detailEntity.自定义7)
                    + "/" + (detailEntity.自定义8 == null ? "" : detailEntity.自定义8)
                    + "/" + (detailEntity.库存自定义1 == null ? "" : detailEntity.库存自定义1)
                    + "/" + (detailEntity.库存自定义2 == null ? "" : detailEntity.库存自定义2)
                    + "/" + (detailEntity.库存自定义3 == null ? "" : detailEntity.库存自定义3)
                    + "/" + (detailEntity.库存自定义4 == null ? "" : detailEntity.库存自定义4)
                    + "/" + (detailEntity.库存自定义5 == null ? "" : detailEntity.库存自定义5)
                    + "/" + (detailEntity.颜色 == null ? "" : detailEntity.颜色)
            ;
            detailEntity.总规格 = tSpk;

            //在制流转条码
            barcodeEntity.状态 = "IN";
            barcodeEntity.仓位 = posEntity.仓位编号;
            barcodeEntity.仓库编号 = posEntity.仓库编号;

            //在制流转条码记录
            在制流转条码记录 recEntity = new 在制流转条码记录();
            recEntity.条码 = barcodeEntity.条码;
            recEntity.流程单号 = barcodeEntity.流程单号;
            recEntity.流转时间 = DateTime.Now;
            recEntity.发出工序 = barcodeEntity.工序系统编号;
            recEntity.收入工序 = "";
            recEntity.数量 = barcodeEntity.数量;
            recEntity.重量 = barcodeEntity.重量;
            recEntity.单据编号 = no;
            recEntity.创建人 = uid;
            if (HeadDAL.inStorage(headEntity, detailEntity, barcodeEntity, recEntity))
            {
                return "true";
            }
            else {
                return "入库失败";
            }
            
        }

        public string getNextSeq(string startWith)
        {
            string ym = DateTime.Now.ToString("yyMM");
            startWith = startWith + ym;
            Expression<Func<成品出入库主表, bool>> where = (p => p.单据编号.StartsWith(startWith));
            Expression<Func<成品出入库主表, object>> order = (p => p.单据编号);
            成品出入库主表 newT = HeadDAL.GetMaxEntiy(where, order);
            string sNext;
            if (newT == null)
            {
                sNext = startWith + "0001";
            }
            else
            {
                sNext = startWith + StringHelper.GetNext(newT.单据编号, 4);
            }
            return sNext;
        }
    }
}
